package cn.farm.farm.Controller.OrdinaryController;


import cn.farm.farm.MessageModel.Common;
import cn.farm.farm.MessageModel.SolrData;
import cn.farm.farm.MessageModel.SolrMessage;
import cn.farm.farm.Solr.MySolr;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SolrController {

    @Autowired
    MySolr mySolr;

    @ResponseBody
    @RequestMapping(value = "farm/ordinary/solr")
    public Common select(String name, @RequestParam(required = false,defaultValue = "0") String start, @RequestParam(required = false)String rows){
        start = "0";
        if(name == null){
            return new SolrMessage(400,"请传入参数",new SolrData(0,0,0,null));
        }
        SolrDocumentList solrDocumentList;
        try{
            solrDocumentList = mySolr.select(name, start, rows);

        }catch (Exception e){
            return new Common(500,"server error");
        }
        return new SolrMessage(200,"ok",new SolrData(solrDocumentList.size(),solrDocumentList.getNumFound(),Integer.parseInt(start),solrDocumentList));
    }
}